library(htmltools)
APP_URL <- "https://pchen-aitutor.hf.space" # ← your Gradio app URL
USER_ID <- "anonymous_user"
browsable(tagList(
# --- styles (fab, bubble, modal) ---
tags$style(HTML("
#tutor-fab{position:fixed;right:20px;bottom:20px;z-index:99999;padding:12px 14px;border-radius:999px;border:1px solid #ddd;background:#fff;box-shadow:0 8px 24px rgba(0,0,0,.15);cursor:pointer;font-weight:600}
#tutor-fab:hover{background:#f6f8ff}
#tutor-sel-btn{position:absolute;display:none;z-index:100000;padding:6px 10px;font-size:12px;border:1px solid #e5e7eb;border-radius:10px;background:#111827;color:#f9fafb;box-shadow:0 8px 24px rgba(0,0,0,.2);cursor:pointer;user-select:none}
#tutor-modal-overlay{display:none;position:fixed;z-index:99998;inset:0;background:rgba(0,0,0,.35)}
#tutor-modal{position:absolute;top:5%;left:50%;transform:translateX(-50%);width:min(980px,95vw);height:min(720px,90vh);background:#fff;border-radius:10px;box-shadow:0 10px 30px rgba(0,0,0,.25);overflow:hidden}
#tutor-modal-header{display:flex;justify-content:space-between;align-items:center;padding:10px 14px;background:#f5f5f7;border-bottom:1px solid #e5e5ea;font-weight:600}
#tutor-close-btn{cursor:pointer;border:none;background:transparent;font-size:18px}
#tutor-iframe{width:100%;height:calc(100% - 18px);border:0}
")),
# modal + iframe
tags$div(id="tutor-modal-overlay",
tags$div(id="tutor-modal",
tags$div(id="tutor-modal-header","AI Calculus Tutor", tags$button(id="tutor-close-btn","✕")),
tags$iframe(id="tutor-iframe", src="")
)
),
# floating button + selection bubble
tags$button(id="tutor-fab","Ask Tutor"),
tags$button(id="tutor-sel-btn","Ask Tutor"),
# script
tags$script(HTML(sprintf("
(function(){
const APP_URL = '%s';
const USER_ID = '%s';
function sel(){ try { return (window.getSelection?window.getSelection().toString():'').trim(); } catch(e){ return ''; } }
function openInline(q, action){
const p = new URLSearchParams({ user_id: USER_ID, q: q||'', action: action||'prefill' });
const frame = document.getElementById('tutor-iframe');
frame.src = APP_URL + '/?' + p.toString(); // Prefill handled by app from URL
document.getElementById('tutor-modal-overlay').style.display='block';
}
function openTab(q, action){
const p = new URLSearchParams({ user_id: USER_ID, q: q||'', action: action||'prefill' });
window.open(APP_URL + '/?' + p.toString(), 'ai_tutor_tab', 'noopener');
}
// floating button: prefill by default; Alt=send; Ctrl/Cmd=new tab
document.getElementById('tutor-fab').addEventListener('click', function(ev){
const q = sel();
const act = ev.altKey ? 'send' : 'prefill';
if (ev.ctrlKey || ev.metaKey) openTab(q, act); else openInline(q, act);
});
// selection bubble near highlighted text
const b = document.getElementById('tutor-sel-btn');
let last='';
function place(x,y){ b.style.left=(x+10)+'px'; b.style.top=(y+10)+'px'; }
function show(){ b.style.display='block'; } function hide(){ b.style.display='none'; }
document.addEventListener('mouseup', e => setTimeout(() => {
const t = sel(); if (t && t.length>=2 && t.length<=800){ last=t; place(e.pageX,e.pageY); show(); } else hide();
}, 20));
window.addEventListener('scroll', hide, {passive:true}); window.addEventListener('resize', hide);
// bubble click: prefill by default; Alt=send; Ctrl/Cmd=new tab
b.addEventListener('click', function(ev){
if (!last) return;
const act = ev.altKey ? 'send' : 'prefill';
if (ev.ctrlKey || ev.metaKey) openTab(last, act); else openInline(last, act);
hide();
});
// close modal
const overlay = document.getElementById('tutor-modal-overlay');
document.getElementById('tutor-close-btn').addEventListener('click', () => overlay.style.display='none');
overlay.addEventListener('click', e => { if (e.target===overlay) overlay.style.display='none'; });
})();
", APP_URL, USER_ID)))
))By the end of this 90-minute session, students will be able to:
R skills
function()Bean & Brew Café wants to optimize its operations using mathematical modeling. The owner needs to understand:
The owner asks: “How can I use R to visualize these relationships and find the optimal production level?”
In R, functions can be defined using several approaches:
Method 1: Vectorised operations in R
By vectorised operation we mean the variable \(x\) is a sequence of real numbers as we did in the previous section. In mathematics we call such \(x\) a vector. Vectorised operation creates a new sequence \(y\). It matches to each number in the sequence of \(x\) a number in the new sequence of \(y\).
R code for a function defined by vectorised operation is like follows, which we have used in the previous section.
Method 2: Using function() keyword
R code using function() is like follows:
For \[y = 0.5x + 2\]
The following R code defines the function and plot its graph.
f <- function(x) {
return(0.5*x+2)
}
x <- seq(-4,8,1)
y <- f(x)
plot(x, y, type = "l", main = "Linear Function Y = 0.5 X + 2", xlab = "X", ylab = "Y")Often the vectorized operation method is simple but it is less flexible and in some situation it is not applicable.
The price is a piece-wise linear function.
\[C(d) = \begin{cases} 10 & \text{if } 0 \leq d \leq 5 \\ 10 + 2(d-5) & \text{if } 5 < d \leq 15 \\ 10 + 20 + 1.5(d-15) & \text{if } d > 15 \end{cases}\]
In this case we can only use function() to define the
price function in R.
C<-function(d){
if ((d>0)&( d <= 5)) {price<-10}
if ((d>5)&( d <= 15)) {price<- 10 + 2*(d-5)}
if (d>15) {price <- 10 + 20 + 1.5*(d-15)}
return(price)
}
d<- seq(1,30)
P<-d*0
# here we cannot do P <- D(d) because the condition in function C(d) refers d as a single number not sequence.
# hence we match each component of P to the corresponding component of d one by one
for (i in 1:length(d)) {
P[i] = C(d[i])
}
plot(d,P,type = "l", main = "Price of Delevery",
xlab = "Distance in KM", ylab = "Price")Use R keyword function() to define the following
function and draw its graph over the domain of [0,10] in following R
Environment or in your own local RStudio. You can copy the sample code
above and modify it to define the function.
\[f(x) = \begin{cases} 2x & \text{if } 0 \leq x \leq 5 \\ 10 + 4(x-5) & \text{if } 5 < x \\ \end{cases}\]
Plot a function using the function as input
R function() allows more types of input than real
numbers. We take function as input to generate a desired output. In this
sense function() extends the concept of real functions.
Here is an example of how to define a Plot function that takes a
function and a vector variable as input, and gives the graph of the
function over the domain given by the vector as output.
Plot_function <- function(f,x) {
y <- f(x)
plot(x,y,type="l")
}
### note: Plot_function can be used to plot any function of $x$. See following two examples
## we define a function Line_point_slope: this function goes through the point (x0,y0) with a slope equals b
x0 = 3
y0 = 4
b = 2
Line_point_slope <- function(x){
y = y0 + b*(x-x0)
return(y)
}
## we define a function Polynomial3: this polynomial is of order 3
Polynomial3 <- function(x){
y = x^3+2*x^2-4*x-1
return(y)
}
x <- seq(0,10,0.1)
Plot_function(Line_point_slope,x)Use R keyword function() to define a function that use a
yet to be specified function \(f\) as
input and draw the graphs of \(f\) over
a subset of the domain of \(f\) in the
following R Environment or in your own local RStudio. Then specify
different function \(f\) and draw the
graph of \(f\).
A linear function that goes through a point \((x_0,y_0)= (2,3)\) and has the slope \(b=1.5\).
\(f(x) = \sin(2x)\),
\(f(x)=\frac{1}{1+e^-x}\),
\(f(x) =\frac{x^3+1}{5+x^2}\))
The R code of the two graphs above can be used as a template to complete this exercise.
InnovateTech Corp is optimizing its operations across multiple dimensions:
The CEO asks: “How can we visualize these multivariable relationships in R to make better strategic decisions?”
A function \[f : \mathbb{R}^n \to \mathbb{R}\] is called a multivariable function if it maps multiple inputs \((x_1,x_2,...,x_n)\) to a real number \(f(x_1,x_2,...,x_n)\).
Example of a linear function \[f(x,y) = x + 2y - 4\] Thsi function takes two inputs \(x\) and \(y\) and return a value that depends lienarly on \(x\) and \(y\).
Example of a quadratic function \[f(x,y) =x^2+y^2\] This function takes two inputs \(x\) and \(y\) and returns their squared sum. This is a function of two variables.
Multivariable Function in R
In plotting a univariable function \(y = f(x)\),
In plotting a multivariable function \(z=f(x,y)\) we follow the same procedure.
\[f(x,y) = x+ 2y -4\] A function of two variables can be visualised as a surface over in a 3D coordinate system, where \((x,y)\) are the input variables and \(z\) is the value of the output variable. For the linear function the surface will be a plane.
As in the case of univariate functions, we create sequences for the input variables \(x\) and \(y\). For each pair \((x,y)\) we calculate the function value \(z=f(x,y)\). The resulting \(z\)-values are then connected to form the surface.
Surface plot
x <- seq(-10, 10, length.out = 30)
y <- seq(-10, 10, length.out = 30)
f <- function(x, y) { Q <- x + 2*y - 4 }
z <- outer(x, y, f)
#op <- par(bg = "white")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")Contour Plot
One often used visualisation of a multivariable function is the contour plot. The following link shows a contour plot of Snowing Mountain in googlemap.
Rice fields on hills create contour lines (level lines) in the landscape. Since rice needs water, the ridges around the fields must be kept even.
The lines of seats in a stadium are similar to the contour lines of a plane.
Along the contour line the height is a constant, therefore contour lines are also called level lines. They are also known as isoquants. Perpendicular to the contour lines is the direction of the steepest descending or ascending.
Heat map
Heat map is often used in weather forecast to visualise the the temperature function over an area. It is a function of two variables the longitude and the latitude. The color corresponds to the function value, the temperature.
For the linear function in the exampl:
Gradient fieled
While the heat map with contour lines show the along which direction the surface is more flat, it shows at same time that the direction perpendicular to the contour lines is the direction of ascending of descending. Gradient field is a plot that shows which direct is the most ascending direction at each point with an arrow.
plot(1, type = "n", xlim = range(x), ylim = range(y),
xlab = "Labor (L)", ylab = "Capital (K)",
main = "Gradient Field of Q(L,K)")
# Define partial derivatives (gradient components)
dQ_dx <- function(x, y) {
1 # ∂Q/∂L
}
dQ_dy <- function(x, y) {
2 # ∂Q/∂K
}
for(l in x) {
for(k in y) {
arrows(l, k,
l + dQ_dx(l, k)/8,
k + dQ_dy(l, k)/8,
length = 0.04, col = "blue")
}
}
On a plane, the direction of maximum ascent is the same everywhere;
therefore, the arrows are identical at each point.
Interactive surface
Interactive surface plot allows to see the 3D object from different perspective and make it a “real” 3D plot.
Slice
Slice is a 2D plot generated by assigning other input variables some constant values. In this way the function value depends only on one input variable. This method is oftne used in ecomonics to investigate the impact on one paritcula variables on the multivariable function.
# --- Slice Plot ---
plot(x, f(x, y = 0), type = "l", col = "blue", lwd = 2,
xlab = "Labour (L)", ylab = "Output", main = "Slice of Production Function (y = 0,2,4)")
lines(x, f(x, y = 2), col = "red", lwd = 2)
lines(x, f(x, y = 4), col = "green", lwd = 2)
legend("bottomright", legend = c("K = 0", "K = 2","K = 4"), col = c("blue", "red","green"), lwd = 2)\[Q(L,K)=20x^{0.7}y^{0.3}\]
Surface plot
L <- seq(0, 10, length.out = 30)
K <- seq(0, 10, length.out = 30)
f <- function(L, K) { Q <- 20*L^0.7*K^0.3 }
z <- outer(L, K, f)
#op <- par(bg = "white")
persp(L, K, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")Contour Plot
Heat Map
Gradient Field
# Define partial derivatives (gradient components)
dQ_dL <- function(L, K) {
20 * 0.7 * L^(-0.3) * K^0.3 # ∂Q/∂L
}
dQ_dK <- function(L, K) {
20 * 0.3 * L^0.7 * K^(-0.7) # ∂Q/∂K
}
plot(1, type = "n", xlim = range(L), ylim = range(K),
xlab = "Labor (L)", ylab = "Capital (K)",
main = "Gradient Field of Q(L,K)")
for(l in L) {
for(k in K) {
arrows(l, k,
l + dQ_dL(l, k)/100,
k + dQ_dK(l, k)/100,
length = 0.04, col = "blue")
}
}
In a gradient field plot, the direction of an arrow points to the
direction of ascent, and the length of the arrow indicates the steepness
of that ascent. The longer the arrow, the steeper the ascent.
Interactive surface
Slice
# --- Slice Plot ---
plot(L, f(L, K = 5), type = "l", col = "blue", lwd = 2,
xlab = "Labour (L)", ylab = "Output", main = "Slice of Production Function (K = 5)")
lines(L, f(L, K = 2), col = "red", lwd = 2)
lines(L, f(L, K = 1), col = "green", lwd = 2)
legend("bottomright", legend = c("K = 5", "K = 2","K = 1"), col = c("blue", "red","green"), lwd = 2)Visualise the following functions using surface plot, contour plot, heat map, slice, and 3D interactive surface plot.
a plane that goes through \((x_0,y_0) = (2,3)\) with slope a=1.5 in x direct and slope b=3 in y direction. (Hint: the plane function is \(f(x,y) = f(x_0,y_0) + a(x-x_0) + b(y-y_0)\)
\(f(x,y) = x^2+y^2\)
using the following R Environment or your own local RStudio.
Finding where supply equals demand:
This requires us to solve this system of two linear equations at the same time.
TechProd Manufacturing produces three products: smartphones (S), tablets (T), and laptops (L). The company needs to determine optimal production levels based on resource constraints.
Resource Requirements per Unit:
Labor hours: 2S + 3T + 4L = 1000 hours available
Materials (kg): 1S + 2T + 3L = 600 kg available
Machine time: 3S + 1T + 2L = 800 hours available
This real-world problem naturally required us to solve this system of 3 linear equations at same time.
How do we want to proceed? Recall how we solve a single linear equation:
\[ax+b=0\] Procedure:
Step 1: Move the unknown to the right hand side and the known to the left hand side. \[ax = -b\] Step 2: Multiply the inverse of the coefficient \(a\) to both side of the equation.
\[a^{-1}ax = a^{-1}(-b)\] This leads the thesolution: \[x = -a^{-1}b\] For \(a=3\) and \(b=2\), R solution is as follows:
## [,1]
## [1,] -0.6666667
Can we follow the same procedure to solver a system of linear equations? The answer is yes. Here is how.
Example 2.4: The Manufacturing Company
step 1: Move all unknowns to the left and side of the equation and the knowns to the right hand side of the equation, and arrange the unknown variables in the same order: i.e. \(S\) first, \(T\) second, and \(L\) third
\[2S + 3T + 4L = 1000\] \[1S + 2T + 3L = 600\] \[3S + 1T + 2L = 800\] step 1.1
Put all unkonwns into one sequence as a vector \(X=\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)\)
and the knowns on the right hand side as a vector \(b=\left( \begin{array}{c} 1000\\ 600\\ 800\\ \end{array} \right)\)
step 1.2
Put the coefficients of the equation system into a matrix;
\(A=\left( \begin{array}{ccc} 2&3&4\\ 1&2&3\\ 3&1&2\\ \end{array} \right)\)
Note the for a 3 equations system, we have 3 unknowns, i.e. \(X\) has 3 elements: \(X=\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)\); the right hand side knowns \(b\) also has 3 elements: \(b=\left( \begin{array}{c} 1000\\ 600\\ 800\\ \end{array} \right)\);
the coefficient matrix is a \(3 \times 3\) square matrix, the values in the matrix correspond to their positions in the three equations. ( This is why the order of the variables in the equation is sensitive.)
\[\underbrace{\left( \begin{array}{rrr} 2 & 3 & 4\\ 1 & 2 & 3\\ 3&1 & 2\\ \end{array} \right)}_{A} \underbrace{\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)}_{X} = \underbrace{\left( \begin{array}{r} 1000\\ 600\\ 800\\ \end{array} \right)}_{b} \]
Step 2 Multiply the inverse of the coefficient \(A\) to both side of the equation
\[A^{-1}AX = A^{-1}b\] This leads to the solution \(A^{-1}A=I\):
\[X = A^{-1}b\]
R solution is as follows:
## [,1]
## [1,] 2.000000e+02
## [2,] 2.000000e+02
## [3,] 5.684342e-14
The solution is \(S=200\), \(T=200\), and \(L=0\).
Example 1.4: The Market Equilibrium (Revisited)
After moving the unknowns to the left hand side, We have in matrix form:
\[\underbrace{\left( \begin{array}{rr} 1 & -2 \\ 1 & 1 \\ \end{array} \right)}_{A} \underbrace{\left( \begin{array}{c} Q\\ P\\ \end{array} \right)}_{X} = \underbrace{\left( \begin{array}{r} -100\\ 300\\ \end{array} \right)}_{b} \]
R solution
## [,1]
## [1,] 166.6667
## [2,] 133.3333
This is exactly the same solution we have obtained by substitution method in the previous section.
Solve the market equilibrium problem
above using R Environment below or using your own local RStudio.
A system of \(m\) linear equations in \(n\) unknowns has the form: \[\begin{cases} a_{11}x_1 + a_{12}x_2 + \cdots + a_{1n}x_n = b_1 \\ a_{21}x_1 + a_{22}x_2 + \cdots + a_{2n}x_n = b_2 \\ \vdots \\ a_{m1}x_1 + a_{m2}x_2 + \cdots + a_{mn}x_n = b_m \end{cases}\]
This system can be written compactly as: \(\mathbf{Ax} = \mathbf{b}\)
Where: - \(\mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}\) ,\(\hspace{0.5cm}\) \(\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix}\) ,\(\hspace{0.5cm}\) \(\mathbf{b} = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_m \end{bmatrix}\)
\[\mathbf{x} = \mathbf{A}^{-1}\mathbf{b}\]
We have been using vector operations to define a function in R Since last section. The example of solving linear equation systems using matrix notation demonstrates the usefulness of vector and matrix notations. Now we want to formalise these operations to see what kind other benefits these vector and matrix notation can give.
Properties: - Associative: \((\mathbf{AB})\mathbf{C} = \mathbf{A}(\mathbf{BC})\) - Distributive: \(\mathbf{A}(\mathbf{B} + \mathbf{C}) = \mathbf{AB} + \mathbf{AC}\) - Not commutative: \(\mathbf{AB} \neq \mathbf{BA}\) (in general)
Two vectors are parallel to each oether: \(\mathbf{x} = \lambda \mathbf{y}\) or \[\begin{bmatrix} x_1 \\ x_2 \end{bmatrix}=\lambda \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}\].
Two vectors are perpendicular to each other: \(\mathbf{x}'\mathbf{y} = 0\) or \[\begin{bmatrix} x_1 & x_2 \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}=0\].
A vector with \(n\) elements is called an \(n\)-vector. An matrix with \(m\) rows and \(n\) columns is called an \(m\times n\)-matrix. Verify the following statement using the Matrix operation rules above.
For a square matrix \(\mathbf{A}\), the inverse matrix \(\mathbf{A}^{-1}\) satisfies: \[\mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}\]
where \(\mathbf{I}\) is the identity matrix.
\(\mathbf{A}^{-1}\) exists if and only if: 1. \(\mathbf{A}\) is square (\(n \times n\)) 2. \(\det(\mathbf{A}) \neq 0\) (A is non-singular)
Geometric explanation: determinant=0 implies lines/planes are parallel.
Using pencil and paper for 2×2 matrices: \[\mathbf{A}^{-1} = \frac{1}{\det(\mathbf{A})} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}\] where \(\mathbf{A} = \begin{bmatrix} a & b \\ c & d \end{bmatrix}\) and \(\det(\mathbf{A}) = ad - bc\)
For \(\mathbf{A} = \begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix}\), we have following the formula: \(\mathbf{A}^{-1} = \begin{bmatrix} 5 & -3 \\ -3 & 2 \end{bmatrix}\),
Using R
## [1] 1
## [,1] [,2]
## [1,] 5 -3
## [2,] -3 2
Computation of an inverse matrix is doable with pencil and paper only for very small matrixes such as \(2\times 2\) matrices. For large matrices it is always done with computer.
Calculate the determinant and the inverse of \(\mathbf{A} = \begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix}\) using the following R Environment or your local RStudio.
Solving a nonlinear equation system is a much more complicated task than solving a linear equation system. Following the geometric interpretation, one nonlinear equation with two variables is a curve on the (x,y) plane. A two equations system of two variables consists of two curves. If the two curves intersect all intersection points are solutions. We can use the following interactive graph to explore the solutions of a two nonlinear equations system of two variables.
In business and economics the following function is of particular interest \[f(x) = ax^2\]
Question: When is this function always positive (except at \(x=0\))? (e.g. the business activity will grantee a positive result)
Answer: When \(a > 0\)
This is the one variable case of positive definiteness: a function that always curves upwards and has a minimum at 0.
Extending this to multivariate cases, now consider \[f(\bf{x}) = \bf{x}'A \bf{x}= \sum_{i=1}^n\sum_{j=1}^n a_{i,j}x_ix_j\] where \(\bf{x}\) is a vector and \(A\) is a square \(n\times n\) matrix. This is the multivariate version of the scalar quadratic function. If this function is always positive except \(\bf{x=0\), then \(A\) is called positive definite.
In physics, the potential energy stored in a stretched spring is always positive (unless there’s no displacement):
\[ E = \frac{1}{2}\bf{x}'K\bf{x}\] where \(K\) is a stiffness matrix. For the system to make physical sense, energy must be positive \(\to\) \(K\) must be positive definite.
Global Portfolio Management is analyzing investment risk using quadratic forms. The firm’s risk model for a two-asset portfolio is:
\[\text{Risk} = w_1^2\sigma_1^2 + w_2^2\sigma_2^2 + 2w_1w_2\sigma_{12}\]
Where: + \(w_1, w_2\) are portfolio weights
\(\sigma_1^2, \sigma_2^2\) are asset variances
\(\sigma_{12}\) is the covariance between assets
This can be written as a quadratic form: \(\text{Risk} = \mathbf{w}^T\mathbf{\Sigma}\mathbf{w}\) with \(\mathbf{w}=\left( \begin{array}{c} w_1\\ w_2\\ \end{array} \right)\) and \(\mathbf{\Sigma} =\left(\begin{array}{cc} \sigma_1^2& \sigma_{12}\\ \sigma_{12}& \sigma_2^2\\ \end{array} \right)\)
The risk manager asks: “How can we determine if our covariance matrix ensures the risk is always positive (positive definite), and how do we visualize this?”
A symmetric matrix \(\mathbf{A}\) is positive definite if: \[\mathbf{x}^T\mathbf{A}\mathbf{x} > 0 \text{ for all } \mathbf{x} \neq \mathbf{0}\]
Equivalent conditions:
All eigenvalues of \(\mathbf{A}\) are positive
All leading principal minors are positive
\(\mathbf{A} = \mathbf{L}\mathbf{L}^T\) for some invertible matrix \(\mathbf{L}\) (Cholesky decomposition)
## [,1] [,2]
## [1,] 2 1
## [2,] 1 3
## eigen() decomposition
## $values
## [1] 3.618034 1.381966
##
## $vectors
## [,1] [,2]
## [1,] 0.5257311 -0.8506508
## [2,] 0.8506508 0.5257311
# Create a grid over [-10, 10]
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)
z <- outer(x, y, function(x, y) {
mapply(function(x, y) {
X <- c(x, y)
t(X) %*% A %*% X
}, x, y)
})
filled.contour(x, y, z)Interactive surface
A symmetric matrix \(\mathbf{A}\) is negative definite if: \[\mathbf{x}^T\mathbf{A}\mathbf{x} < 0 \text{ for all } \mathbf{x} \neq \mathbf{0}\]
All eigenvalues of \(\mathbf{A}\) are negative
## [,1] [,2]
## [1,] -2 1
## [2,] 1 -3
## eigen() decomposition
## $values
## [1] -1.381966 -3.618034
##
## $vectors
## [,1] [,2]
## [1,] -0.8506508 -0.5257311
## [2,] -0.5257311 0.8506508
# Create a grid over [-10, 10]
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)
z <- outer(x, y, function(x, y) {
mapply(function(x, y) {
X <- c(x, y)
t(X) %*% A %*% X
}, x, y)
})
filled.contour(x, y, z)Interactive surface
For \(A =\begin{bmatrix} -4 & -1 \\ -1 & -6 \end{bmatrix}\) create 3D plots of the quadratic function \(f(\bf{x}) =\bf{x}'A\bf{x}\) over the domain of \(x_1 \in [-1,1]\),\(x_2\in [-1,1]\) using the following R environment of using your own local Rstudio.
This section provides review and application questions designed to assess understanding at five different levels. Each level builds upon the previous one, fostering deeper comprehension and the ability to apply concepts in increasingly complex situations.
https://pchen-exercisesfeedbackselect.hf.space
This comprehensive Section 2 covers the essential mathematical foundations for business analysis:
Preparation for Section 3: These tools provide the foundation for understanding derivatives, which measure rates of change and enable optimization in business contexts.
Key Takeaways: - Mathematics provides powerful tools for business decision-making - Multiple representations (algebraic, graphical, computational) enhance understanding - R programming enables sophisticated analysis of real business problems - Interactive learning reinforces theoretical concepts with practical applications
Total estimated lecture time: 90 minutes Interactive elements: 6 quizzes + 6 R environments Assessment: 30 comprehensive questions across 5 levels of understanding